perm filename INIMF.88[MF,DEK] blob sn#749680 filedate 1984-04-08 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00037 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00004 00002	@x Tell WEAVE to print only the changes:
C00005 00003	@x WAITS's banner:
C00006 00004	@x Switches for debugging and statistics:
C00009 00005	@x The INIMF switch:
C00012 00006	@x Compiler directives:
C00013 00007	@x Compile-time constants:
C00021 00008	@x TANGLE-time constants:
C00024 00009	@x System-dependent character set changes:
C00026 00010	@x Opening files:
C00030 00011	@x New input_ln:
C00037 00012	@x Terminal I/O:
C00040 00013	@x Special terminal controls:
C00042 00014	@x Initializing the terminal:
C00048 00015	@x Making special characters printable:
C00049 00016	@x Terminal input:
C00050 00017	@x The `E' option:
C00054 00018	@x Changes for 36-bit machines:
C00056 00019	@x Eliminating addition/subtraction of zero:
C00058 00020	@x Date and time:
C00060 00021	@x Special classes for SAIL character set goodies
C00064 00022	@x screen routines:
C00068 00023	@x Page number maintenance:
C00070 00024	@x Printing the page number:
C00071 00025	@x More page number maintenance:
C00074 00026	@x Pausing on input:
C00080 00027	@x Parsing file names:
C00091 00028	@x Printing file names:
C00092 00029	@x Converting file names to PASCAL strings:
C00095 00030	@x Parsing file names in the buffer:
C00102 00031	@x The real file names:
C00106 00032	@x Line editor gets misspelled file name:
C00108 00033	@x Reading the first line of a file:
C00111 00034	@x The GF output buffer:
C00115 00035	@x "r GFtoDVI":
C00118 00036	@x The endgame:
C00123 00037	@x Final system-dependent changes:
C00136 ENDMK
C⊗;
@x Tell WEAVE to print only the changes:
	\def\?##1]{\hbox to 1in{\hfil##1.\ }}
	}
@y
	\def\?##1]{\hbox{Changes to \hbox to 1em{\hfil##1}.\ }}
	}
\let\maybe=\iffalse
@z
@x WAITS's banner:
@d banner=='This is METAFONT, Version -88.0' {printed when \MF\ starts}
@y
@d banner=='This is METAFONT, WAITS Version -88.0' {printed when \MF\ starts}
@z
@x Switches for debugging and statistics:
@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering
	usage statistics}
@d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering
	usage statistics}
@f stat==begin
@f tats==end
@y
Moreover, this version of \MF\ has been instrumented so that runtime
frequency counts can be accumulated from all usage of \MF\ over a
long period of time. Such code is delimited by `$|freq|\ldots|qerf|$'.

@d debug== {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@> {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat== {change this to `$\\{stat}\equiv\.{@@\{}$' when not
	gathering usage statistics}
@d tats==@t@> {change this to `$\\{tats}\equiv\.{@@\}}$' when not
	gathering usage statistics}
@f stat==begin
@f tats==end
@#
@d freq==@{ {change this to `$\\{freq}\equiv\.{@@\{}$' when not
	accumulating frequency counts}
@d qerf==@t@>@} {change this to `$\\{qerf}\equiv\.{@@\}}$' when not
	accumulating frequency counts}
@f freq==begin
@f qerf==end
@z
@x The INIMF switch:
@d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version}
@d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version}
@y
@d init== {change this to `$\\{init}\equiv\null$' for \.{INIMF}}
@d tini== {change this to `$\\{tini}\equiv\null$' for \.{INIMF}}
@z
@x Compiler directives:
@{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
@y
@{@&$C-,A+,D-,W+@}
	{no range check, catch arithmetic overflow, no debug overhead}
@!freq @{@&$D:2@}@+ qerf {and even the frequency counters}
@!debug @{@&$C+,D:7,W+@}@+ gubed {but turn everything on when debugging}
{the `|W+|' switch catches more syntax errors}
{the `\ignorespaces|D:5|' avoids initial stop for the debugger}
{the `\ignorespaces|D:2|' augments debugger to maintain counters}
@z
@x Compile-time constants:
@!mem_max=30000; {greatest index in \MF's internal |mem| array;
	must be strictly less than |max_halfword|}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files; must not exceed |max_halfword|}
@!error_line=72; {width of context lines on terminal error messages}
@!half_error_line=42; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=79; {width of longest text lines output; should be at least 60}
@!screen_width=768; {number of pixels in each row of screen display}
@!screen_depth=1024; {number of pixels in each column of screen display}
@!stack_size=30; {maximum number of simultaneous input sources}
@!param_size=30; {maximum number of simultaneous macro parameters}
@!max_strings=1500; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
	available for the user's identifier names and strings,
	after \MF's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all identifiers;
	must exceed |string_vacancies| by the total
	length of \MF's own strings, which is currently about xxx}
@!move_size=5000; {space for storing moves in a single octant}
@!gf_buf_size=800; {size of the output buffer, must be a multiple of 8}
@!file_name_size=40; {file names shouldn't be longer than this}
@!pool_name='TeXformats:MF.POOL                      ';
	{string of length |file_name_size|; tells where the string pool appears}
@.TeXformats@>
@!path_size=100; {maximum number of knots between breakpoints of a path}
@y
@!mem_max=30000; {greatest index in \MF's internal |mem| array;
	must be strictly less than |max_halfword|}
@!buf_size=500; {maximum number of characters simultaneously present in
	current lines of open files; must not exceed |max_halfword|}
@!error_line=72; {width of context lines on terminal error messages}
@!half_error_line=42; {width of first lines of contexts in terminal
	error messages; should be between 30 and |error_line-15|}
@!max_print_line=79; {width of longest text lines output; should be at least 60}
@!screen_width=492; {number of pixels in each row of \.{MFDD} screen display}
@!screen_depth=456; {number of pixels in each column of \.{MFDD} screen display}
@!stack_size=30; {maximum number of simultaneous input sources}
@!param_size=30; {maximum number of simultaneous macro parameters}
@!max_strings=1500; {maximum number of strings; must not exceed |max_halfword|}
@!string_vacancies=8000; {the minimum number of characters that should be
	available for the user's identifier names and strings,
	after \MF's own error messages are stored}
@!pool_size=32000; {maximum number of characters in strings, including all
	error messages and help texts, and the names of all identifiers;
	must exceed |string_vacancies| by the total
	length of \MF's own strings, which is currently about xxx}
@!move_size=5000; {space for storing moves in a single octant}
@!gf_buf_size=800; {size of the output buffer, must be a multiple of 8}
@!file_name_size=23; {file names shouldn't be longer than this}
@!pool_name='MF.POOL[MF,SYS]        ';
	{string of length |file_name_size|; tells where the string pool appears}
@!path_size=100; {maximum number of knots between breakpoints of a path}
@!count_name='<TEX!>.TXT[TEX,SYS]    '; {frequency counts go here}
@!check_period=10; {minutes between checking the upper segment integrity}
@!write_period=12; {|check_period|'s between updates to the count history file}
@z
@x TANGLE-time constants:
@d mem_base=0 {smallest index in the |mem| array, must not be less
	than |min_halfword|}
@d hi_mem_base=13000 {smallest index in the single-word area of |mem|,
	must be substantially larger than |mem_base| and smaller than |mem_max|}
@d hash_size=2100 {maximum number of symbolic tokens,
	must be less than |max_halfword-3*param_size|}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d max_in_open=6 {maximum number of input files and error insertions that
	can be going on simultaneously}
@y
@d mem_base=0 {smallest index in the |mem| array, must not be less
	than |min_halfword|}
@d hi_mem_base=13000 {smallest index in the single-word area of |mem|,
	must be substantially larger than |mem_base| and smaller than |mem_max|}
@d hash_size=2100 {maximum number of symbolic tokens,
	must be less than |max_halfword-3*param_size|}
@d hash_prime=1777 {a prime number equal to about 85\% of |hash_size|}
@d max_in_open=6 {maximum number of input files and error insertions that
	can be going on simultaneously}
@z
@x System-dependent character set changes:
@↑character set dependencies@>
@↑system dependencies@>

@<Set init...@>=
for i←1 to @'37 do xchr[i]←' ';
@y
@↑character set dependencies@>
@↑system dependencies@>

The code shown here is intended to be used on the Stanford {\sc SAIL} system,
and at other installations like CMU and ISI where essentially the same
extended character set is used. The fact that {\mc SAIL} has |'}'| in the
wrong place turns out to cause no difficulty in this case.

@<Set initial values...@>=
for i←1 to @'37 do xchr[i]←chr(i);
xchr[@'30]←chr(@'137);
xchr[@'32]←chr(@'33); {|not_equal| sign}
xchr[@'33]←chr(@'176);
@z
@x Opening files:
@d reset_OK(#)==erstat(#)=0
@d rewrite_OK(#)==erstat(#)=0

@p function a_open_in(var @!f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/O'); a_open_in←reset_OK(f);
end;
@#
function a_open_out(var @!f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O'); a_open_out←rewrite_OK(f);
end;
@#
function b_open_in(var @!f:byte_file):boolean;
	{open a binary file for input}
begin reset(f,name_of_file,'/O'); b_open_in←reset_OK(f);
end;
@#
function b_open_out(var @!f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O'); b_open_out←rewrite_OK(f);
end;
@#
function w_open_in(var @!f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var @!f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O'); w_open_out←rewrite_OK(f);
end;
@y
@d reset_OK(#)==erstat(#) mod @'20000 = 0
@d rewrite_OK(#)==erstat(#) mod @'20000 = 0

@p function erstat(var @!f:file):integer; extern;@t/2@>
@#
function a_open_in(var @!f:alpha_file):boolean;
	{open a text file for input}
begin reset(f,name_of_file,'/E/O/N:9');
	{the \.{/E} switch distinguishes |form_feed| from |carriage_return|;
	the \.{/O} switch gives error control to us;
	and the \.{/N:9} switch specifies 9 buffers, which
	seems to work satisfactorily at {\mc SAIL}}
a_open_in←reset_OK(f);
end;
@#
function a_open_out(var @!f:alpha_file):boolean;
	{open a text file for output}
begin rewrite(f,name_of_file,'/O/N:2'); a_open_out←rewrite_OK(f);
end; {two buffers seems adequate for text output files}
@#
function b_open_in(var @!f:byte_file):boolean;
	{open a binary file for input}
begin reset(f,name_of_file,'/B:8/O/N:2'); b_open_in←reset_OK(f);
end;	{the \.{/B} switch is necessary to get byte packing}
@#
function b_open_out(var @!f:byte_file):boolean;
	{open a binary file for output}
begin rewrite(f,name_of_file,'/O/N:9'); b_open_out←rewrite_OK(f);
end;    {here we use |ary_out| so the \.{/B} switch isn't appropriate}
@#
function w_open_in(var @!f:word_file):boolean;
	{open a word file for input}
begin reset(f,name_of_file,'/O/N:9'); w_open_in←reset_OK(f);
end;
@#
function w_open_out(var @!f:word_file):boolean;
	{open a word file for output}
begin rewrite(f,name_of_file,'/O/N:9'); w_open_out←rewrite_OK(f);
end;
@z
@x New input_ln:
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables called
|buffer|, |first|, and |last| that will be described in detail later; for
now, it suffices for us to know that |buffer| is an array of |ASCII_code|
values, and that |first| and |last| are indices into this array
representing the beginning and ending of a line of text.

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@y
@ Input from text files is read one line at a time, using a routine called
|input_ln|. This function is defined in terms of global variables
called |buffer|, |first|, and |last| that will be described in detail
later; for now, it suffices for us to know that |buffer| is an array of
|ASCII_code| values, and that |first| and |last| are indices into this
array representing the beginning and ending of a line of text.

We will read the lines first into an auxiliary buffer, in order to
save the running time of procedure-call overhead. This uses a nice
feature of \ph\ that Knuth chose not to mention in \MF84.
@↑Knuth, Donald Ervin@>

At {\mc SAIL} we want to recognize page marks (indicated by |form_feed|
characters), and keep track of the current page number.

@d form_feed=@'14 {ASCII code used at end of a page}

@<Glob...@>=
@!buffer:array[0..buf_size] of ASCII_code; {lines of characters being read}
@!first:0..buf_size; {the first unused position in |buffer|}
@!last:0..buf_size; {end of the line just input to |buffer|}
@!max_buf_stack:0..buf_size; {largest index used in |buffer|}
@!aux_buf:array[0..70] of text_char; {where the characters go first}
@↑system dependencies@>
@z
@x
@p function input_ln(var @!f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
var @!last_nonblank:0..buf_size; {|last| with trailing blanks removed}
begin if bypass_eoln then if not eof(f) then get(f);
	{input the first character of the line into |f↑|}
last←first; {cf.\ Matthew 19:30}
if eof(f) then input_ln←false
else	begin last_nonblank←first;
	while not eoln(f) do
		begin if last≥max_buf_stack then
			begin max_buf_stack←last+1;
			if max_buf_stack=buf_size then
				overflow("buffer size",buf_size);
@:METAFONT capacity exceeded buffer size}{\quad buffer size@>
			end;
		buffer[last]←xord[f↑]; get(f); incr(last);
		if buffer[last-1]≠" " then last_nonblank←last;
		end;
	last←last_nonblank; input_ln←true;
	end;
end;
@y
@p function input_ln(var @!f:alpha_file;@!bypass_eoln:boolean):boolean;
	{inputs the next line or returns |false|}
label 1,done;
var @!n: integer;
@!k,@!m: 0..buf_size; {indices into |buffer|}
begin if bypass_eoln then {input the first character of the line into |f↑|}
	begin if not eof(f) then get(f);
	if not eof(f) then if f↑=chr(@'12) then get(f); {skip past a |line_feed|}
	end;
last←first;
if eof(f) then input_ln←false
else	begin read(f,aux_buf:n);
	if buffer[first]=form_feed then {previous line was end-of-page}
		begin incr(page); line←1; {adjust line and page numbers}
		end;
1:	if last+n>max_buf_stack then
		if last+n≥buf_size then
			begin max_buf_stack←buf_size;
			overflow("buffer size",buf_size);
@:METAFONT capacity exceeded buffer size}{\quad buffer size@>
			end
		else max_buf_stack←last+n;
	if n>0 then
		begin m←last;
		if n=72 then last←m+71@+else last←m+n;
		for k←m to last-1 do buffer[k]←xord[aux_buf[k-m]];
		if n=72 then {there's more on this line}
			begin read(f,aux_buf:n); goto 1;
			end;
		end
	else if f↑=chr(form_feed) then {end of page}
		begin aux_buf[0]←f↑; n←1; goto 1;
		end;
	loop@+	begin if last=first then goto done;
		if buffer[last-1]≠" " then goto done;
		decr(last);
		end;
done:	input_ln←true;
	end;
end;
@↑system dependencies@>
@z
@x Terminal I/O:
is considered an output file the file variable is |term_out|.
@↑system dependencies@>

@<Glob...@>=
@!term_in:alpha_file; {the terminal as an input file}
@!term_out:alpha_file; {the terminal as an output file}

@ Here is how to open the terminal files
in \ph. The `\.{/I}' switch suppresses the first |get|.
@↑system dependencies@>

@d t_open_in==reset(term_in,'TTY:','/O/I') {open the terminal for text input}
@d t_open_out==rewrite(term_out,'TTY:','/O') {open the terminal for text output}
@y
is considered an output file the file variable is |term_out|.
On WAITS, this point is moot, since we use the built-in |TTY| file.
@↑system dependencies@>

@d term_in==TTY {the terminal as an input file}
@d term_out==TTY {the terminal as an output file}

@ Here is how to open the terminal files on WAITS: we don't do anything,
since |TTY| is always open.  Note that |eoln(term_in)| is initially |true|.
@↑system dependencies@>

@d t_open_in==do_nothing {open the terminal for text input}
@d t_open_out==do_nothing {open the terminal for text output}
@z
@x Special terminal controls:
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d wake_up_terminal == do_nothing {cancel the user's cancellation of output}
@y
@d clear_terminal == break_in(term_in,true) {clear the terminal input buffer}
@d tty_set==@'400121 {special instruction to {\mc WAITS}}
@d tty_escape_code==@'004000000000 {to simulate user typing ESC}
@d tty_break_code==@'004000000400 {to simulate user typing BREAK}

@<Error handling procedures@>=
function inskp0:boolean; extern;
procedure wake_up_terminal;
var @!val:integer; {a value that we don't have to look at}
@!success:boolean; {ditto}
begin begin if inskp0 then end; {cancel the user's cancellation of output}
esc_break[1]:=tty_escape_code + ord('N'); {simulate ESC \.N}
call_i(tty_set,-1,esc_break,val,success);
end;
@z
@x Initializing the terminal:
@ The following program does the required initialization
without retrieving a possible command line.
It should be clear how to modify this routine to deal with command lines,
if the system permits them.
@↑system dependencies@>

@p function init_terminal:boolean; {gets the terminal input started}
label exit;
begin t_open_in;
loop@+begin wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	write_ln(term_out,'Please type the name of your input file.');
	end;
exit:end;
@y
@ The following program does the required initialization
and accepts interrupts and also retrieves a possible command line, using
new system routines due to David R. Fuchs.
@↑Fuchs, David Raymond@>

@d pto_chr(#)==ptwr1w(0,ord(#)) {put a character in the line editor}

@p procedure esci(var @!x:integer); extern; @t\2@>@;
	{increments |x| each time the user types escape-I or break-I;
	the program can change |x| whenever it wants to, but |x| had
	better be a global variable}
@#
function rescan:boolean; extern; @t\2@>@;
	{puts the command line into the terminal buffer,
	or returns |false| if there was no command line}
@#
function tmp_in(f:s@&t@&r@&i@&n@&g;var @!s:s@&t@&r@&i@&n@&g):integer; extern;
	@t\2@>@;
	{reads \.{TMPCOR} file |f| into |s|, and returns its length
		(|≤0| means error)}
@#
function cclsw: boolean; extern; @t\2@>@;
	{was program started with \.{RUN} offset of 1 (i.e., from \.{SNAIL})?}
@#
procedure ptwr1w(pty,c:integer); extern; @t\2@>@;
	{simulates typing of a character on a \.{PTY}}
@#
function init_terminal:boolean; {gets the terminal files started}
label exit;
var @!l:integer; {length returned by |tmp_in|}
@!line_found:boolean; {have we scanned a line?}
@!tmp_cor_buf:packed array[0..100] of char; {where |tmp_in| puts things}
begin t_open_in;
esci(interrupt);
last←first;
if cclsw then {started by \.{MF} monitor command}
	begin l←tmp_in('MF',tmp_cor_buf);
	loc←1;
	while (loc<l)∧(tmp_cor_buf[loc]≠'←') do incr(loc);
	incr(loc);
	while loc<l do
		begin if tmp_cor_buf[loc]>' ' then
			begin buffer[last]←xord[tmp_cor_buf[loc]]; incr(last);
			end;
		incr(loc);
		end;
	end
else
@!debug if false then@;@+gubed@;@/
if rescan then
	begin read_ln(term_in); {get first character into |term_in↑|}
	while (¬ eoln(term_in))∧(term_in↑≠';') do get(term_in);
	if term_in↑=';' then
		begin get(term_in);
		while ¬ eoln(term_in) do
			begin buffer[last]←xord[term_in↑]; incr(last); get(term_in);
			end;
		end;
	end;
line_found←(last>first);
loop@+	begin loc←first;
	while (loc<last)∧(buffer[loc]=" ") do incr(loc);
	if loc<last then
		begin init_terminal←true;
		return; {return unless the line was all blank}
		end;
	if line_found then
		write_ln(term_out,'Please type the name of your input file.');
	wake_up_terminal; write(term_out,'**'); update_terminal;
@.**@>
	buffer[first]←0; {|input_ln| may look at |buffer[first]|}
	if not input_ln(term_in,true) then {this shouldn't happen}
		begin write_ln(term_out);
		write(term_out,'! End of file on the terminal... why?');
@.End of file on the terminal@>
		init_terminal←false; return;
		end;
	line_found←true;
	end;
exit:end;
@↑system dependencies@>
@z
@x Making special characters printable:
@<Character |k| cannot be printed@>=
	(k<" ")∨(k>"~")
@y
@<Character |k| cannot be printed@>=
	(k=@'177)∨(k in [0,@'11..@'15,@'33])
@z
@x Terminal input:
begin update_terminal; {Now the user sees the prompt for sure}
@y
begin update_terminal; {Now the user sees the prompt for sure}
buffer[first]←0; {makes sure |input_ln| doesn't find a |form_feed|}
@z
@x The `E' option:
line ready to be edited. But such an extension requires some system
wizardry, so the present implementation simply types out what file should be
edited and the relevant line number.
@y
line ready to be edited. The present implementation does this by loading
the line editor with the appropriate call to the editor. We treat `\.T' the
same as `\.E', because other programs on this system invoke the editor
when the user says `\.T'.
@z
@x
"E": if file_ptr>0 then
	begin print_nl("You want to edit file ");
@.You want to edit file x@>
	print(input_stack[file_ptr].name_field);
	print(" at line "); print_int(line);
	interaction←scroll_mode; jump_out;
	end;
@y
"E","T": if file_ptr>0 then
	begin selector←new_string; pool_ptr←str_start[str_ptr];
	print("et "); print(input_stack[file_ptr].name_field);
	print_char("/"); print_int(page); print("p/");
	print_int(line); print_char("l"); print_char(@'15);
	if str_ptr<max_strings then
		begin pseudo_typein←str_ptr; incr(str_ptr);
		str_start[str_ptr]←pool_ptr;
		end; {|make_string| not declared |forward|}
	selector←term_and_log; interaction←scroll_mode; jump_out;
	end;
@z
@x Changes for 36-bit machines:
The values defined here are recommended for most 32-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=255 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==65535 {largest allowable value in a |halfword|}
@y
The values defined here are recommended for most 36-bit computers.

@d min_quarterword=0 {smallest allowable value in a |quarterword|}
@d max_quarterword=511 {largest allowable value in a |quarterword|}
@d min_halfword==0 {smallest allowable value in a |halfword|}
@d max_halfword==262143 {largest allowable value in a |halfword|}
@z
@x Eliminating addition/subtraction of zero:
@ The operation of subtracting |min_halfword| occurs rather frequently in
\MF, so it is convenient to abbreviate this operation by using the macro
|ho| defined here.  \MF\ will run faster with respect to compilers that
don't optimize the expression `|x-0|', if this macro is simplified in the
obvious way when |min_halfword=0|.
@↑system dependencies@>

@d ho(#)==#-min_halfword
	{to take a sixteen-bit item from a halfword}
@y
@ The operation of subtracting |min_halfword| occurs rather frequently in
\MF, so it is convenient to abbreviate this operation by using the macro
|ho| defined here.  \MF\ will run faster with respect to compilers that
don't optimize the expression `|x-0|', if this macro is simplified in the
obvious way when |min_halfword=0|. So it has been simplified in the obvious way.
@↑system dependencies@>

@d ho(#)==# {to take a sixteen-bit item from a halfword}
@z
@x Date and time:
Since standard \PASCAL\ cannot provide such information, something special
is needed. The program here simply specifies July 4, 1776, at noon; but
users probably want a better approximation to the truth.

Note that the values are |scaled| integers. Hence \MF\ can no longer
be used after the year 4095.

@p procedure fix_date_and_time;
begin internal[time]←12*60*unity; {minutes since midnight}
internal[day]←4*unity; {fourth day of the month}
internal[month]←7*unity; {seventh month of the year}
internal[year]←1776*unity; {Anno Domini}
end;
@y
It uses a {\mc WAITS} monitor call that puts the date in the left 18 bits
and the time in the right 18 bits.

@p procedure fix_date_and_time;
var @!t:integer; {accumulator}
date:integer; {raw date}
g:boolean; {garbage}
begin call_i(@'400101,,t,t,g); {that's \.{ACCTIM}}
date←t div @'1000000;
internal[time]←((t mod @'1000000) div 60)*unity;
internal[day]←((date mod 31)+1)*unity;
internal[month]←(((date div 31) mod 12)+1)*unity;
internal[year]←((date div (31*12))+1964)*unity;
if internal[month]=@'1000000 then if internal[day]=unity then {April Fool}
	wterm_ln('Hello! I am your user-friendly MF System.');
end;
@z
@x Special classes for SAIL character set goodies
for k←0 to " " do char_class[k]←space_class;
@y
for k←0 to " " do char_class[k]←space_class;
char_class[@'30]←10; {left arrow will join the class of \.{:=}}
char_class[@'32]←10; {not equal sign, likewise}
char_class[@'34]←10; {less than or equal sign as well}
char_class[@'35]←10; {greater than or equal sign too}
char_class[form_feed]←max_class+1; {form feed will be a special class}
@z
@x screen routines:
@p function init_screen:boolean;
begin init_screen←false;
end;
@#
procedure update_screen; {will be called only if |init_screen| returns |true|}
begin confusion("u");
@:this can't happen u}{\quad u@>
end;
@y
@p function INITSC:boolean; extern;@t\2@>@/
function init_screen:boolean;
var @!b:boolean;
begin print_nl("Calling INITSC:");
b←INITSC;
if b then print("true")@+else print("false");
init_screen←b;
end;
procedure UPDSCR; extern;
procedure update_screen;
begin print_nl("Calling UPDSCR");
UPDSCR;
end;
@z
@x
@p procedure blank_rectangle(@!left_col,@!right_col:screen_col;
	@!top_row,@!bot_row:screen_row);
var @!r:screen_row;
@!c:screen_col;
begin @{@+for r←top_row to bot_row-1 do
	for c←left_col to right_col-1 do
		screen_pixel[r,c]←white;@+@}@/
confusion("b"); {|blank_rectangle| is called only after |init_screen=true|}
@:this can't happen b}{\quad b@>
end;
@y
@p procedure BLANKR(@!left_col,@!right_col:screen_col;
	@!top_row,@!bot_row:screen_row); extern;@t\2@>@/
procedure blank_rectangle(@!left_col,@!right_col:screen_col;
	@!top_row,@!bot_row:screen_row);
begin print_nl("Calling BLANKR("); print_int(left_col); print_char(",");
print_int(right_col); print_char(","); print_int(top_row); print_char(",");
print_int(bot_row); print_char(")");
BLANKR(left_col,right_col,top_row,bot_row);
end;
@z
@x
@p procedure paint_row(@!r:screen_row;@!b:pixel_color;var @!a:trans_spec;
	@!n:screen_col);
var @!k:screen_col; {an index into |a|}
@!c:screen_col; {an index into |screen_pixel|}
begin @{ k←0; c←a[0];
repeat incr(k);
	repeat screen_pixel[r,c]←b; incr(c);
	until c=a[k];
	b←black-b; {$|black|\swap|white|$}
	until k=n;@+@}@/
confusion("p"); {|paint_row| is called only after |init_screen=true|}
@:this can't happen p}{\quad p@>
end;
@y
@p procedure PAINTR(@!r:screen_row;@!b:pixel_color;var @!a:trans_spec;
	@!n:screen_col); extern;@t\2@>@/
procedure paint_row(@!r:screen_row;@!b:pixel_color;var @!a:trans_spec;
	@!n:screen_col);
var @!k:screen_col; {an index into |a|}
begin print_nl("Calling PAINTR(");
print_int(r); print_char(","); print_int(b); print_char(";");
for k←0 to n do
	begin print_int(a[k]); if k≠n then print_char(",");
	end;
print_char(")");
PAINTR(r,b,a,n);
end;
@z
@x Page number maintenance:
If more information about the input state is needed, it can be
included in small arrays like those shown here. For example,
the current page or segment number in the input file might be
put into a variable |@!page|, maintained for enclosing levels in
`\ignorespaces|@!page_stack:array[1..max_in_open] of integer|\unskip'
by analogy with |line_stack|.
@y
Similarly, we maintain a global variable |page| and a corresponding
|page_stack|.
@z
@x
@!line_stack : array[1..max_in_open] of integer;
@y
@!line_stack : array[1..max_in_open] of integer;
@!page : integer; {current page number in the current source file}
@!page_stack : array[1..max_in_open] of integer;
@z
@x Printing the page number:
else	begin print_nl("l."); print_int(line);
@y
else	begin if page>1 then
		begin print_nl("p."); print_int(page); print(",l.");
		end
	else print_nl("l.");
	print_int(line);
@z
@x More page number maintenance:
or |limit| or |line|.
@y
or |limit| or |line| or |page|.
@z
@x
line_stack[index]←line; start←first;
@y
line_stack[index]←line; start←first; page_stack[index]←page;
@z
@x
begin first←start; line←line_stack[index];
@y
begin first←start; page←page_stack[index]; line←line_stack[index];
@z
@x Pausing on input:
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected, each line of input is displayed
on the terminal and the transcript file, followed by `\.{=>}'.
\MF\ waits for a response. If the response is null (i.e., if nothing is
typed except perhaps a few blank spaces), the original
line is accepted as it stands; otherwise the line typed is
used instead of the line in the file.

@p procedure firm_up_the_line;
var @!k:0..buf_size; {an index into |buffer|}
begin limit←last;
if (internal[pausing]>0)∧(interaction>nonstop_mode) then
	begin wake_up_terminal; print_ln;
	if start<limit then for k←start to limit-1 do print(buffer[k]);
	first←limit; prompt_input("=>"); {wait for user response}
@.=>@>
	if last>first then
		begin for k←first to last-1 do {move line down in buffer}
			buffer[k+start-first]←buffer[k];
		limit←start+last-first;
		end;
	end;
end;
@y
@ If the user has set the |pausing| parameter to some positive value,
and if nonstop mode has not been selected,
each line of input is displayed in the transcript file, followed by `\.{=>}',
and also put into the user's line-editor buffer.
\MF\ waits for the line to be edited, and the next line received is
used instead of the line in the file.

@p procedure firm_up_the_line;
var @!k:0..buf_size; {an index into |buffer|}
begin limit←last;
if (internal[pausing]>0)∧(interaction>nonstop_mode)∧@|
		(buffer[start]≠form_feed) then
	begin wake_up_terminal; print_ln;
	if start=limit then {empty line will be made nonempty so that it's visible}
		begin buffer[start]←" "; incr(limit);
		end;
	decr(selector); {inhibit terminal output temporarily}
	for k←start to limit-1 do
		begin print_char(buffer[k]);
		pto_chr(xchr[buffer[k]]);
		end;
	print("=>"); first←start;
	if not input_ln(term_in,true) then
		fatal_error("End of file on the terminal!");
@.End of file on the terminal@>
	if last>first then for k←first to last-1 do print_char(buffer[k]);
	limit←last; print_ln; incr(selector);
	end;
end;
@z
@x Parsing file names:
@ The file names we shall deal with for illustrative purposes have the
following structure:  If the name contains `\.>' or `\.:', the file area
consists of all characters up to and including the final such character;
otherwise the file area is null.  If the remaining file name contains
`\..', the file extension consists of all such characters from the first
remaining `\..' to the end, otherwise the file extension is null.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.>' or `\.:', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@y
@ The file names we shall deal with have the following structure:
If the name contains `\.[', the file area consists of all characters
from this character to the end; otherwise the file area is null.
If the remaining file name contains `\..', the file extension consists of all
such characters from this character to the end, otherwise the file extension
is null. We can assume that there is at most one `\.[' and at most one `\..'.
@↑system dependencies@>

We can scan such file names easily by using two global variables that keep track
of the occurrences of area and extension delimiters:

@<Glob...@>=
@!area_delimiter:pool_pointer; {the most recent `\.[', if any}
@!ext_delimiter:pool_pointer; {the relevant `\..', if any}
@z
@x
@d MF_area=="MFinputs:"
@.MFinputs@>
@y
@d MF_area=="[tex,sys]"
@z
@x
else	begin if (c=">")∨(c=":") then
		begin area_delimiter←pool_ptr; ext_delimiter←0;
		end
	else if (c=".")∧(ext_delimiter=0) then ext_delimiter←pool_ptr;
@y
else	begin if c="[" then area_delimiter←pool_ptr
	else if c="." then ext_delimiter←pool_ptr;
@z
@x
if area_delimiter=0 then cur_area←""
else	begin cur_area←str_ptr; incr(str_ptr);
	str_start[str_ptr]←area_delimiter+1;
	end;
if ext_delimiter=0 then
	begin cur_ext←""; cur_name←make_string;
	end
else	begin cur_name←str_ptr; incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←make_string;
	end;
@y
cur_name←str_ptr;
if ext_delimiter=0 then cur_ext←""
else	begin incr(str_ptr);
	str_start[str_ptr]←ext_delimiter; cur_ext←str_ptr;
	end;
if area_delimiter≤str_start[str_ptr] then
	begin cur_area←""; incr(str_ptr); str_start[str_ptr]←pool_ptr;
	end
else	begin incr(str_ptr);
	str_start[str_ptr]←area_delimiter; cur_area←make_string;
	end;
@z
@x Printing file names:
begin print(a); print(n); print(e);
@y
begin print(n); print(e); print(a);
@z
@x Converting file names to PASCAL strings:
allows both lowercase and uppercase letters in the file name.
@↑system dependencies@>

@d append_to_name(#)==begin c←#; incr(k);
@y
converts lowercase letters to uppercase.

A special convention is used here with respect to font metric files: If the
file name is longer than six characters (e.g., `\.{helvetica}' or
`\.{oldenglish}'), we abbreviate it by retaining the first three and
last three characters (e.g., `\.{helica}' or `\.{oldish}').
@↑system dependencies@>

@d append_to_name(#)==begin c←#; incr(k);
	if (c≥"a")∧(c≤"z") then c←c-@'40; {convert to uppercase}
@z
@x
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
@y
if (e=".tfm")∧(length(n)>6) then
	begin for j←str_start[n] to str_start[n]+2 do
		append_to_name(str_pool[j]);
	for j←str_start[n+1]-3 to str_start[n+1]-1 do
		append_to_name(str_pool[j]);
	end
else for j←str_start[n] to str_start[n+1]-1 do append_to_name(str_pool[j]);
for j←str_start[e] to str_start[e+1]-1 do append_to_name(str_pool[j]);
for j←str_start[a] to str_start[a+1]-1 do append_to_name(str_pool[j]);
@z
@x Parsing file names in the buffer:
@d base_default_length=21 {length of the |MF_base_default| string}
@d base_area_length=11 {length of its area part}
@d base_ext_length=5 {length of its `\.{.base}' part}
@y
@d base_default_length=18 {length of the |MF_base_default| string}
@d base_area_length=9 {length of its area part}
@d base_ext_length=4 {length of its `\.{.base}' part}
@z
@x
MF_base_default←'TeXformats:PLAIN.base';
@.TeXformats@>
@y
MF_base_default←'PLAIN.bas[tex,sys]';
@z
@x
|MF_base_default|.
@y
|MF_base_default|; but it actually switches stuff around to keep the
file area last.
@z
@x
@!j:integer; {index into |buffer| or |MF_base_default|}
@y
@!j:integer; {index into |buffer| or |MF_base_default|}
@!d:integer; {a kludge}
@z
@x
for j←1 to n do append_to_name(xord[MF_base_default[j]]);
for j←a to b do append_to_name(buffer[j]);
for j←base_default_length-base_ext_length+1 to base_default_length do
	append_to_name(xord[MF_base_default[j]]);
@y
for j←a to b do append_to_name(buffer[j]);
if b=0 then
	begin d←base_default_length-base_area_length+1;
	n←base_default_length;
	end
else d←base_default_length-base_area_length-base_ext_length+1;
for j←d to base_default_length-base_area_length do
	append_to_name(xord[MF_base_default[j]]);
for j←base_default_length-n+1 to base_default_length do
	append_to_name(xord[MF_base_default[j]]);
@z
@x The real file names:
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
which simply makes a \MF\ string from the value of |name_of_file|, should
ideally be changed to deduce the full name of file~|f|, if it is
possible to do this in a \PASCAL\ program.
@↑system dependencies@>

@p function make_name_string:str_number;
var @!k:1..file_name_size; {index into |name_of_file|}
begin str_room(name_length);
for k←1 to name_length do append_char(xord[name_of_file[k]]);
make_name_string←make_string;
end;
function a_make_name_string(var @!f:alpha_file):str_number;
begin a_make_name_string←make_name_string;
end;
function b_make_name_string(var @!f:byte_file):str_number;
begin b_make_name_string←make_name_string;
end;
function w_make_name_string(var @!f:word_file):str_number;
begin w_make_name_string←make_name_string;
end;
@y
@ Operating systems often make it possible to determine the exact name (and
possible version number) of a file that has been opened. The following routine,
due to David Fuchs, deduces the full name of file~|f| on {\mc WAITS}:
@↑system dependencies@>
@↑Fuchs, David Raymond@>

@p procedure cur_nam(var @!chan:f@&i@&l@&e;var @!s:string); extern; @t\2@>@/
function make_name_string(var @!f:f@&i@&l@&e):str_number;
var @!s:packed array[1..24] of char;
@!k:1..24; {file names at {\sc SAIL} have at most 23 characters}
begin cur_nam(f,s); str_room(24); k←1;
while ord(s[k])≠0 do
	begin append_char(xord[s[k]]); incr(k);
	end;
make_name_string←make_string;
end;
function a_make_name_string(var @!f:alpha_file):str_number;
begin a_make_name_string←make_name_string(f);
end;
function b_make_name_string(var @!f:byte_file):str_number;
begin b_make_name_string←make_name_string(f);
end;
function w_make_name_string(var @!f:word_file):str_number;
begin w_make_name_string←make_name_string(f);
end;
@z
@x Line editor gets misspelled file name:
begin if interaction=scroll_mode then wake_up_terminal;
@y
@!i:pool_pointer; {index into |str_pool|}
begin if interaction=scroll_mode then wake_up_terminal;
@z
@x
clear_terminal; prompt_input(": "); @<Scan file name in the buffer@>;
@y
clear_terminal; {now we'll fill the line editor's buffer with the old name}
for i←str_start[cur_name] to str_start[cur_name+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_ext] to str_start[cur_ext+1]-1 do
	pto_chr(xchr[str_pool[i]]);
for i←str_start[cur_area] to str_start[cur_area+1]-1 do
	pto_chr(xchr[str_pool[i]]);
ptwr1w(0,@'214); {control-formfeed returns cursor to start of line}
prompt_input(": "); @<Scan file name in the buffer@>;
@z
@x Reading the first line of a file:
@ Here we have to remember to tell the |input_ln| routine not to
start with a |get|. If the file is empty, it is considered to
contain a single blank line.
@↑system dependencies@>

@<Read the first line...@>=
begin if ¬ input_ln(cur_file,false) then do_nothing;
@y
@ Here we have to remember to tell |input_ln| not to do the first |get|,
and we also want to skip over the material on a file directory page. An
empty file is considered to contain a single blank line.
@↑system dependencies@>

@<Read the first line...@>=
begin if input_ln(cur_file,false) then
	begin if(last-start=29)∧(buffer[start]="C")∧(buffer[start+8]=@'26) then
		begin while (cur_file↑≠chr(form_feed))∧(not eof(cur_file)) do
			begin read_ln(cur_file); read(cur_file,aux_buf:temp_ptr);
			end; {skip the directory}
		buffer[start]←form_feed; last←start+1;
		end;
	end;
page←1;
@z
@x The GF output buffer:
@!gf_index=0..gf_buf_size; {an index into the output buffer}
@y
@!gf_index=0..gf_buf_size; {an index into the output buffer}
@!packed_bytes=packed array[gf_index] of eight_bits;
	{buffer for \.{GF} output}
@z
@x
@!gf_buf:array[gf_index] of eight_bits; {buffer for \.{GF} output}
@y
@!gf_buf:packed_bytes; {buffer for \.{GF} output}
@z
@x
procedure write_gf(@!a,@!b:gf_index);
var k:gf_index;
begin for k←a to b do write(gf_file,gf_buf[k]);
end;
@y
procedure ary_out(var @!f:file;@!b:packed_bytes; @!o,@!c:integer);
	extern;@t\2@>@#
procedure write_gf(@!a,@!b:gf_index);
begin ary_out(gf_file,gf_buf,a div 4,(b+1-a)div 4);@{+1000@}
end;
@z
@x "r GFtoDVI":
	b_close(gf_file);
@y
	b_close(gf_file);
	if pseudo_typein=0 then if internal[proofing]>0 then
		begin k←selector; selector←new_string;
		pool_ptr←str_start[str_ptr];
		print("r GFtoDVI;"); print(output_file_name);
		selector←k;
		if pool_ptr<pool_size then
		 if str_ptr<max_strings then {|overflow| can't occur}
			pseudo_typein←make_string;
		end;
@z
@x The endgame:
@<Last-minute...@>=
@y
The new stuff at {\mc SAIL} has to do with preparing for what the user
presumably wants to do next, by typing it for him/her.

@<Last-minute...@>=
@z
@x
end;
@y
if (pseudo_typein≠0)∧(interaction>batch_mode) then
	begin write_ln(term_out);
	for k←str_start[pseudo_typein] to str_start[pseudo_typein+1]-1 do
		pto_chr(xchr[str_pool[k]]);
	end;
end;
@z
@x Final system-dependent changes:
This section should be replaced, if necessary, by changes to the program
that are necessary to make \MF\ work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the published program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@↑system dependencies@>
@y
Here are the remaining things needed to make the implementation
complete at {\mc SAIL}.
@↑system dependencies@>

@ The |pseudo_typein| variable is set nonzero if the |error| routine
uses the `\.E' option to exit and edit.

@<Glob...@>=
@!pseudo_typein:str_number;

@ @<Set init...@>=
pseudo_typein←0; page←0;

@ The frequency counting system makes use of two special system routines
devised by David R. Fuchs with the help of Martin Frost and Joe Weening.
It uses the capabilities of ``phantom'' jobs, which are able to keep the
upper segment of \MF\ alive for long periods of time so that statistics
can be accumulated across many runs.
@↑Fuchs, David Raymond@>
@↑Frost, Martin Edward@>
@↑Weening, Joseph Simon@>

There's a boolean function |tex_phn| called by every \MF\ job right at
the beginning; it returns |true| or |false| depending on whether this
\MF\ job should be the counter phantom.  (If the job's \.{JLOG} bit is
off, then the job wants to be a phantom, so we rename it to \.{[TEX!]},
unless there is already such a job, in which case we die.)  If we are the
phantom, |tex_phn| saves away the upper segment name (for later
reference), and calculates a checksum of the upper segment, which it then
stores in the upper segment.  Then |tex_phn| returns, and it is \MF's job
to save its counts and to call |tex_chk| now and then.

Procedure |tex_chk| checks that the upper segment has not been violated.
If it has, |tex_chk| will change the upper segment name (so that no other
lusers will get it), and it also changes the lower segment name (so that
no new \MF's will confuse this one for a good phantom, and so that the
system will also start up a new phantom); furthermore, |tex_chk| will try
to change the code so that all users' jobs will print an error message and
stop; after this, |tex_chk| will halt the phantom (which also makes the
system start a new one).

Another thing that |tex_chk| checks is that its upper segment's name hasn't
changed; otherwise it assumes it has been killed (which causes the segment
name to to be set to \.{*SEG*}), and goes away.  This allows a new version of
\MF\ to be put up by making the \.{TEX.DMP} file, renaming the old count file,
and then killing the old \MF\ upper segment.  This will wipe out current
lusers, however!

If |tex_phn| determines that we are not the phantom, but just a regular \MF\
job, then there is still some work to do.  In particular, we want to make
every effort to see that we're connected to the phantom high segment, so
that our counts are accounted for.  To do this, |tex_phn| looks for a job
named \.{[TEX!]}, and gets that job's high segment number.  If there is not
exactly one \.{[TEX!]}, then |tex_phn| gives up and lets the user run without
being accounted for (unless a phantom starts up subsequently and latches
onto this job's high segment, in which case only the counts from the
latter part of the job will be counted!).  If there is a \.{[TEX!]}, then
|tex_phn| compares its upper segment number to our upper segment number.  If
it's the same, then |tex_phn| just returns, since this job's counts will
count.  Otherwise, |tex_phn| calculates its upper segment's checksum, and
attaches \.{[TEX!]}'s upper segment.  If we find a matching checksum in the
right spot, then |tex_phn| assumes that this upper segment is OK, and lets
the user continue with it.  If the checksum comparison fails, then a nasty
message is printed, informing the user that he is using an outdated \MF\
dump file, and |tex_phn| reattaches to the original upper segment.

@<Last-minute procedures@>=
@!freq
function @!tex_phn:boolean; extern;@t\2@>
procedure @!tex_chk; extern;@t\2@>
qerf

@ Frequency counts go to a special file.

@<Glob...@>=
@!freq @!count_file:alpha_file; {the number of counts, followed by their values}
qerf

@ Here's a type that facilitates system hackery. (\MF\ doesn't actually
use all of these variants.)

@d dec_word==packed record
	case integer of
	0: (@!xx:halfword; @!p: ↑integer);
	1: (@!z: integer);
	2: (@!lh:halfword; @!rh:halfword);
	3: (@!sixbit: packed array[1..6] of 0..@'77);
	4: (@!b: set of bits)
	end

@ A special procedure called |magic| is called near the beginning of \MF,
to do the necessary stuff for frequency counts.

@<Initialize the output routines@>=
@!freq magic;@+qerf

@ And here it is; |magic| does nothing unless this \MF\ has been started up
by the operating system with a bit set that makes it want to be phantom.

@<Last-minute procedures@>=
@!freq procedure magic;
label exit;
mtype @!bits=0..35;
var @!num_counts: integer; {number of counters pointed to in |mem|}
@!job_hrl: integer; {highest address of this job}
@!junk,@!i,@!j: integer; {temporaries}
@!success: boolean; {temporary}
@!hack,@!memry: dec_word; {two more temporaries}
begin if not tex_phn then return; {do nothing if we're not the phantom}
@<Set the value of |job_hrl|
	to the highest address of code in the upper segment@>;
@<Make pointers to the counts@>;
@<Read in the old counts, if possible@>;
@<Forever update the count file, and keep an eye on corruption@>;
exit:end;
qerf

@ @<Set the value of |job_hrl|...@>=
hack.z←@'115; hack.z←hack.p↑; job_hrl←hack.rh

@ The phantom \MF\ uses its |mem| array to point to the frequency
counts that appear in the upper segment, since this \MF\ does no typesetting.
Counters are characterized by the fact that the preceding word contains
the command `\.{AOSA *+1}'.

@d AOSA_code==@'354000 {machine-language op code for \.{AOSA}}

@<Make pointers to the counts@>=
num_counts←0;
memry.z←@'400001;
while memry.z<job_hrl do
	begin hack.z←memry.p↑;
	if (hack.lh=AOSA_code) and (hack.rh=memry.z+1) then
		begin incr(memry.z); {skip to the counter}
		mem[num_counts].int←memry.z; {save address of counter}
		incr(num_counts);
		end;
	incr(memry.z);
	end

@ Since we will look at accumulated counts only when \MF\ has not
changed versions, there is no need to store a check sum with the count file.

@<Read in the old counts, if possible@>=
reset(count_file,count_name,'/O');
if not eof(count_file) then
	begin read_ln(count_file,i);
	if i=num_counts then for i←0 to num_counts-1 do
		begin memry.z←mem[i].int;
		read_ln(count_file,memry.p↑);
		end;
	end;
close(count_file)

@ @<Forever update the count file, and keep an eye on corruption@>=
loop@+	begin for i←1 to write_period do
		begin for j←1 to check_period do
			call_i(@'31,,60,junk,success); {``sleep'' a minute}
		tex_chk; {check the upper segment for corruption}
		end;
	rewrite(count_file,count_name); {now we'll write out the counts}
	write_ln(count_file,num_counts);
	for i←0 to num_counts-1 do
		begin memry.z←mem[i].int;
		write_ln(count_file,memry.p↑:1);
		end;
	close(count_file);
	end

@ Here's a global variable that's needed for our ESC/BREAK hack.

@<Glob...@>=
@!esc_break: array [1..1] of integer;

@ And finally, here's a ``frozen'' definition that makes end-of-page
act like an ``outer'' semicolon.

@<Put each...@>=
text(form_feed+1)←form_feed;
eq_type(form_feed+1)←semicolon+outer_tag;
@z